package com.yjd.api.interceptor;

import com.yjd.comm.base.interceptor.BaseInterceptor;
import com.yjd.comm.base.model.ResultModel;
import com.yjd.comm.enums.ResultCodeEnum;
import com.yjd.comm.user.dto.UserSession;
import com.yjd.comm.user.enums.ClientChannelEnum;
import com.yjd.comm.user.util.UserUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.vo.Constant.ResultEnum;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class MobileInterceptor extends BaseInterceptor {

	public static final String URL_FILE = "nousermobileurl.txt";

	public boolean doPreHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		UserSession user = this.getUser(request, response); // 用户信息
		if (user != null) {
			// 判断有没有在拦截排除列表中
			if (!urlIsNeedValidate(ServletUtil.getRquestUriNotContainContextPath(request))) {
				return true;
			}
			if (user.getMobile_number()==null||user.getMobile_number()==0) {
				ServletUtil.outputJsonMessage(response, new ResultModel(ResultEnum.ERROR, ResultCodeEnum.unbind_mobile.getCode(), "请先绑定手机", null).toMap());
				return false;
			}
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected List<String> getUrlFiles() {
		return FrameUtil.newArrayList(LoginInterceptor.URL_FILE, MobileInterceptor.URL_FILE);
	}

	/**
	 * 获取登陆用户信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	private UserSession getUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return UserUtil.getUserModel(request, response, ClientChannelEnum.MOBILE);
	}

}